package view.views;

import org.eclipse.jdt.core.IJavaElement;

public class JavaElementSpecifier {

	public static String getFullSpecification(IJavaElement element) {
		String specification = element.getAncestor(element.getElementType())
				.toString();
		/* 7 <=> Klasse */
		if (element.getElementType() == 7) { // Klasse vom Benutzer
			specification = replaceInString(specification, '[', '-');
			String[] specificationParts = specification.split("-");
			String[] separateBeginning = specificationParts[0].split(" ");
			if (separateBeginning.length == 2) {
				return specificationParts[0].trim();
			} else { // Java Klasse
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'{', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'=', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'/', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						';', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'}', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'<', ' ');
				separateBeginning[2] = replaceInString(separateBeginning[2],
						'>', ' ');
				String[] javaClass = separateBeginning[2].split(" ");
				javaClass[2] = javaClass[2].substring(1);
				// String returnString = "";
				StringBuilder tmp = new StringBuilder();
				for (int i = 2; i < javaClass.length; i++) {
					if (i == 2) {
						// returnString += javaClass[i];
						tmp.append(javaClass[i]);
					} else {
						// returnString += "."+javaClass[i];
						tmp.append(".");
						tmp.append(javaClass[i]);
					}
				}
				// returnString.trim();
				return tmp.toString().trim();

			}
		}
		/* 9 <=> Methode */
		if (element.getElementType() == 9) {
			specification = replaceInString(specification, '{', '-');
			String[] specParts = specification.split("-");
			if (specParts.length == 1) { // Methode vom Benutzer
				specification = replaceInStringFor14(specification, '[', '-');
				String[] specificationParts = specification.split("-");
				String parentClass = (specificationParts[1].split(" "))[1];
				String[] twoPartOfMethod = separateInTwoParts(specificationParts[0]);
				String returnString = "";
				if (twoPartOfMethod[1].compareTo("") == 0)
					returnString = parentClass + "." + twoPartOfMethod[0];
				else {
					if (twoPartOfMethod[0]
							.charAt(twoPartOfMethod[0].length() - 1) == ',') {
						int pos = specificationParts[0].lastIndexOf(' ');
						returnString = parentClass + "."
								+ specificationParts[0].substring(0, pos);
					} else
						returnString = twoPartOfMethod[0] + " " + parentClass
								+ "." + twoPartOfMethod[1];
				}
				return returnString.trim();
			} else { // Java Methode
				String spec = specParts[0];
				if (spec.split(" ").length > 1) { // Normale Java Methode
					String[] twoParts = separateInTwoParts(specParts[0]);
					String[] secondTwoParts = separateInTwoParts(twoParts[1]);
					String secondReturnString = "";
					if (secondTwoParts[0]
							.charAt(secondTwoParts[0].length() - 1) == ',')
						secondReturnString = secondTwoParts[0] + " "
								+ secondTwoParts[1];
					else
						secondReturnString = secondTwoParts[0] + "."
								+ secondTwoParts[1];
					specParts[1] = replaceInString(specParts[1], '[', '-');
					String[] getClassName = specParts[1].split("-");
					boolean findIn = false;
					int i = 0;
					while (findIn == false) {
						i++;
						String[] tmp = getClassName[i].split(" ");
						if (tmp[0].compareTo("in") == 0)
							findIn = true;
					}
					String[] twoInIt = getClassName[i].split(" ");
					String returnString = "";
					if (twoParts[0].compareTo("static") != 0) {
						returnString = twoParts[0] + " " + twoInIt[1];
						if (secondReturnString.compareTo("") != 0)
							returnString += "." + secondReturnString;
					} else
						returnString = twoInIt[1] + " " + secondReturnString;
					return returnString.trim();
				} else { // Methode als Konstruktor auch von Java
					specParts[1] = replaceInString(specParts[1], '[', '-');
					String[] getComplete = specParts[1].split("-");
					String getName = (getComplete[1].split(" "))[1];
					String getParent = (getComplete[3].split(" "))[1];
					String returnString = getParent + "." + getName + "."
							+ specParts[0];
					return returnString.trim();
				}
			}
		}
		if (element.getElementType() == 8) {
			specification = replaceInString(specification, '[', '-');
			String[] specificationParts = specification.split("-");
			String[] elementName = specificationParts[0].split(" ");
			String[] elementParent = specificationParts[1].split(" ");
			String returnString = elementName[0] + " " + elementParent[1] + "."
					+ elementName[1];
			return returnString.trim();
		}
		if (element.getElementType() == 14) {
			specification = replaceInStringFor14(specification, '[', '-');
			String[] specificationParts = specification.split("-");
			String[] elementName = separateInTwoParts(specificationParts[0]);
			String[] elementParent = separateInTwoParts(specificationParts[1]);
			elementParent[1] = elementParent[1].substring(0,
					elementParent[1].length() - 1);
			String[] elementClass = specificationParts[2].split(" ");
			String returnString = elementName[0] + " " + elementClass[1] + "."
					+ elementParent[1] + "." + elementName[1];
			return returnString.trim();
		}
		return "";
	}

	private static String replaceInStringFor14(String string, char what,
			char with) {
		StringBuilder tmp = new StringBuilder();
		for (int i = 0; i < string.length(); i++) {
			if (string.charAt(i) == what && string.charAt(i - 1) == ' ')
				tmp.append(with);
			else
				tmp.append(string.charAt(i));
		}
		return tmp.toString();
	}

	private static String[] separateInTwoParts(String string) {
		String[] returnArrayString = new String[2];
		boolean find = false;
		int i = 0;
		while (find == false) {
			if (string.charAt(i) == ' ') {
				returnArrayString[0] = string.substring(0, i);
				returnArrayString[1] = string.substring(i + 1, string.length());
				if(returnArrayString[0].compareTo("static") == 0){
					returnArrayString[0] += " void";
					returnArrayString[1] = string.substring(i + 6, string.length());
				}
				find = true;
			}
			i++;
		}
		return returnArrayString;
	}
	
	private static String cutTheSpaces(String string) {
		StringBuilder tmp = new StringBuilder();
		for (int i = 0; i < string.length(); i++) {
			if (string.charAt(i + 1) == ' ') {
				if (string.charAt(i + 2) == string.length())
					return tmp.toString();
				else if (string.charAt(i + 2) == ' ')
					return tmp.toString();
				else
					tmp.append(string.charAt(i));
			}
		}
		return tmp.toString();
	}

	private static String replaceInString(String string, char what, char with) {
		StringBuilder tmp = new StringBuilder();
		for (int i = 0; i < string.length(); i++) {
			if (string.charAt(i) == what)
				tmp.append(with);
			else
				tmp.append(string.charAt(i));
		}
		return tmp.toString();
	}
}
